package me.xplabs.engine 
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author xiaohan
	 */
	public class GameManager 
	{
		private var _stage:Stage;
		private var _functions:Vector.<Function>;
		private var _keys:Dictionary;
		private var _dataUpdates:Vector.<Function>;
		private var _isAddDataUpdate:Boolean;
		private var _isDataUpdate:Boolean;
		public function GameManager() 
		{
		}
		public function setStage(stage:Stage):void
		{
			_stage = stage;
			_keys = new Dictionary();
			_dataUpdates = new Vector.<Function>();
			_functions = new Vector.<Function>();
			//_stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		private function onKeyDown(e:KeyboardEvent):void 
		{
			var tempKeys:Vector.<Function> = _keys[e.keyCode];
			if (!tempKeys) return;
			var len:int = tempKeys.length;
			for (var i:int = 0; i < len; i++) 
			{
				tempKeys[i](e.keyCode, e.ctrlKey, e.shiftKey);
			}
		}
		
		/*private function onKeyUp(e:KeyboardEvent):void 
		{
			isCtrlKey = e.ctrlKey;
		}*/
		/**
		 * 添加按键回调函数
		 * @param	key 按键key值，参见ASCII
		 * @param	func
		 */
		public function setKey(key:int,func:Function):void
		{
			if (!_keys[key]) _keys[key] = new Vector.<Function>();
			_keys[key].push(func);
			
		}
		/**
		 * 删除按键回调函数
		 * @param	key 按键key值，参见ASCII
		 * @param	func
		 * @return
		 */
		public function delKey(key:int, func:Function):Function
		{
			if (_keys[key]) 
			{
				var len:int = _keys[key].length;
				for (var i:int = 0; i < len; i++) 
				{
					if (func == _keys[key][i])
					{
						return _keys[key].splice(i, 1)[0];
					}
				}
			}
			return null;
		}
		
		
		/**
		 * 添加帧循环回调函数
		 * @param	func
		 */
		public function addUpdate(func:Function):void
		{
			var len:int = _functions.length;
			_functions.push(func);
		}
		/**
		 * 删除帧循环回调函数
		 * @param	func
		 * @return
		 */
		public function delUpdate(func:Function):Function
		{
			var len:int = _functions.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (func == _functions[i])
				{
					return _functions.splice(i, 1)[0];
				}
			}
			return null;
		}
		public function addDataUpdate(func:Function):void
		{
			var len:int = _dataUpdates.length;
			_dataUpdates.push(func);
		}
		public function delDataUpdate(func:Function):Function
		{
			var len:int = _dataUpdates.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (func == _dataUpdates[i])
				{
					return _dataUpdates.splice(i, 1)[0];
				}
			}
			return null;
		}

		public function startDataUpdate():void
		{
			//if (_isAddDataUpdate) return;
			//_isAddDataUpdate = true;
			//addUpdate(onDataEnterframe);
			_isDataUpdate = true;
		}

		public function stopDataUpdate():void
		{
			//if (!_isAddDataUpdate) return;
			//_isAddDataUpdate = false;
			//delUpdate(onDataEnterframe);
			_isDataUpdate = false;
		}
		
		private function onDataEnterframe():void 
		{
			var len:int = _dataUpdates.length;
			for (var i:int = 0; i < len; i++) 
			{
				_dataUpdates[i]();
			}
		}
		/**
		 * 开启帧循环
		 */
		public function startUpdate():void
		{
			_stage.addEventListener(Event.ENTER_FRAME, onEnterframe);
		}
		/**
		 * 战艇帧循环
		 */
		public function stopUpdate():void
		{
			_stage.removeEventListener(Event.ENTER_FRAME, onEnterframe);
		}
		private function onEnterframe(e:Event):void 
		{
			var len:int = _functions.length;
			for (var i:int = 0; i < len; i++) 
			{
				_functions[i]();
			}
			if (_isDataUpdate) onDataEnterframe();
		}
	}
}